package cn.itsource.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.system.UserInfo;
import cn.itsource.common.SendMsg_webchinese;
import cn.itsource.pojo.MyEenu.DriverAuthEnum;
import cn.itsource.pojo.domain.DriverAuthMaterial;
import cn.itsource.mapper.DriverAuthMaterialMapper;
import cn.itsource.pojo.ro.DriverAuthMaterialRo;
import cn.itsource.pojo.ro.RealAuthRo;
import cn.itsource.pojo.ro.UserInfoRo;
import cn.itsource.service.IDriverAuthMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 司机实名资料 服务实现类
 * </p>
 *
 * @author whale
 * @since 2024-07-02
 */
@Service
public class DriverAuthMaterialServiceImpl extends ServiceImpl<DriverAuthMaterialMapper, DriverAuthMaterial> implements IDriverAuthMaterialService {

    @Autowired
    private SendMsg_webchinese sendMsgWebchinese;

    @Override
    public void realauth(RealAuthRo realAuthRo) {
        //通过id拿到信息
        DriverAuthMaterial material = super.getById(realAuthRo.getId());
        //判断是否同意
        if (realAuthRo.getApprove()) {
            auth(material, DriverAuthEnum.AUTH_SUCCESS, realAuthRo);
            //发送短信
            String phone = material.getPhone();
            sendMsgWebchinese.sendMessage(phone,"恭喜你，通过了面试");
        }else {
            auth(material, DriverAuthEnum.AUTU__NO_PASS, realAuthRo);
            sendMsgWebchinese.sendMessage(material.getPhone(),realAuthRo.getRemark());
        }

    }

    @Override
    public void realauthCancel(RealAuthRo realAuthRo) {
        DriverAuthMaterial material = super.getById(realAuthRo.getId());
        material.setRealAuthStatus(DriverAuthEnum.TO_DO_AUTH.getStatus());
        material.setCreateTime(new Date());
        material.setAuditUserId(Long.valueOf(StpUtil.getLoginId().toString()));
        material.setAuditRemark(realAuthRo.getRemark());
        material.setUpdateTime(new Date());
        super.updateById(material);

    }

    @Override
    public Boolean getDriverStatus() {
        Object userinfo = StpUtil.getExtra("userinfo");

        //根据id 拿到信息
        DriverAuthMaterial byId = super.getById(Long.valueOf(StpUtil.getLoginId().toString()));
        if (byId != null) {
            Integer status = byId.getRealAuthStatus();
            if (status==1){
                return true;
            }
            return false;
        }else {
            return false;
        }
    }

    /**
     * 上传司机审核信息
     * @param driverAuthMaterialRo
     */
    @Override
    public void save(DriverAuthMaterialRo driverAuthMaterialRo) {
        DriverAuthMaterial driverAuthMaterial = DriverAuthMaterialRo.todriverAuthMaterialRo(driverAuthMaterialRo);
        //审核状态
        driverAuthMaterial.setRealAuthStatus(DriverAuthEnum.TO_DO_AUTH.getStatus());
        super.save(driverAuthMaterial);

    }


    private void auth(DriverAuthMaterial material, DriverAuthEnum authSuccess, RealAuthRo realAuthRo) {
        material.setRealAuthStatus(authSuccess.getStatus());
        material.setCreateTime(new Date());
        material.setAuditUserId(Long.valueOf(StpUtil.getLoginId().toString()));
        material.setAuditRemark(realAuthRo.getRemark());
        material.setDriverId(Long.valueOf(realAuthRo.getId().toString()));
        super.updateById(material);
    }
}
